// 模拟数据服务
const mockUsers = [
  {
    id: 1,
    username: 'admin',
    phone: '13800138000',
    email: 'admin@example.com',
    level: 3,
    status: 1,
    balance: '1000.00',
    points: 500,
    avatar: '',
    realName: '系统管理员',
    address: '北京市朝阳区',
    registerTime: '2024-01-01 10:00:00',
    lastLoginTime: '2024-10-25 09:30:00',
    registerIp: '192.168.1.100'
  },
  {
    id: 2,
    username: 'user001',
    phone: '13800138001',
    email: 'user001@example.com',
    level: 1,
    status: 1,
    balance: '500.50',
    points: 200,
    avatar: '',
    realName: '张三',
    address: '上海市浦东新区',
    registerTime: '2024-02-15 14:20:00',
    lastLoginTime: '2024-10-24 16:45:00',
    registerIp: '192.168.1.101'
  },
  {
    id: 3,
    username: 'user002',
    phone: '13800138002',
    email: 'user002@example.com',
    level: 2,
    status: 0,
    balance: '300.00',
    points: 150,
    avatar: '',
    realName: '李四',
    address: '广州市天河区',
    registerTime: '2024-03-10 09:15:00',
    lastLoginTime: '2024-10-23 11:20:00',
    registerIp: '192.168.1.102'
  }
]

const mockCarousels = [
  {
    id: 1,
    title: '手工编织中国结',
    imageUrl: 'https://via.placeholder.com/750x300/409eff/ffffff?text=中国结推广',
    link: '/products/1',
    sort: 1,
    status: 1,
    startTime: '2024-01-01 00:00:00',
    endTime: '2024-12-31 23:59:59',
    createTime: '2024-01-01 10:00:00',
    remark: '传统手工技艺，简单易学'
  },
  {
    id: 2,
    title: '手工串珠手链',
    imageUrl: 'https://via.placeholder.com/750x300/67c23a/ffffff?text=串珠手链',
    link: '/products/2',
    sort: 2,
    status: 1,
    startTime: '2024-02-01 00:00:00',
    endTime: '2024-12-31 23:59:59',
    createTime: '2024-02-01 14:30:00',
    remark: '精美手工艺品，适合初学者'
  },
  {
    id: 3,
    title: '手工折纸花束',
    imageUrl: 'https://via.placeholder.com/750x300/e6a23c/ffffff?text=折纸花束',
    link: '/products/3',
    sort: 3,
    status: 0,
    startTime: '2024-03-01 00:00:00',
    endTime: '2024-12-31 23:59:59',
    createTime: '2024-03-01 09:15:00',
    remark: '环保材料，寓意美好'
  },
  {
    id: 4,
    title: '新手入门专区',
    imageUrl: 'https://via.placeholder.com/750x300/f56c6c/ffffff?text=新手入门',
    link: '/products?difficulty=1',
    sort: 4,
    status: 1,
    startTime: '2024-01-15 00:00:00',
    endTime: '2024-12-31 23:59:59',
    createTime: '2024-01-15 16:45:00',
    remark: '简单易学的手工艺品推荐'
  }
]

const mockProducts = [
  {
    id: 1,
    name: '手工编织中国结',
    description: '传统手工编织中国结，适合新手学习，工艺简单，成品美观',
    category: '编织类',
    unitPrice: 15.00,
    stock: 200,
    images: [
      'https://via.placeholder.com/300x300/409eff/ffffff?text=中国结成品',
      'https://via.placeholder.com/300x300/67c23a/ffffff?text=编织过程'
    ],
    videoUrl: 'https://via.placeholder.com/video.mp4',
    accessoriesList: [
      { name: '红绳', quantity: '2米', unit: '根' },
      { name: '珠子', quantity: '10颗', unit: '包' },
      { name: '剪刀', quantity: '1把', unit: '把' },
      { name: '胶水', quantity: '1瓶', unit: '瓶' }
    ],
    productionTime: 120, // 制作时间（分钟）
    difficulty: 1, // 难度等级 1-5
    tools: ['剪刀', '胶水', '尺子'],
    status: 1,
    claimedCount: 45,
    completedCount: 32,
    approvedCount: 28,
    createTime: '2024-01-15 10:30:00',
    updateTime: '2024-10-20 15:45:00'
  },
  {
    id: 2,
    name: '手工串珠手链',
    description: '精美手工串珠手链制作，多种颜色可选，适合各种场合佩戴',
    category: '串珠类',
    unitPrice: 12.00,
    stock: 150,
    images: [
      'https://via.placeholder.com/300x300/e6a23c/ffffff?text=串珠手链成品',
      'https://via.placeholder.com/300x300/f56c6c/ffffff?text=串珠过程'
    ],
    videoUrl: 'https://via.placeholder.com/video2.mp4',
    accessoriesList: [
      { name: '彩色珠子', quantity: '50颗', unit: '包' },
      { name: '弹力线', quantity: '1米', unit: '卷' },
      { name: '串珠针', quantity: '1根', unit: '根' },
      { name: '搭扣', quantity: '2个', unit: '个' }
    ],
    productionTime: 60,
    difficulty: 2,
    tools: ['串珠针', '剪刀'],
    status: 1,
    claimedCount: 78,
    completedCount: 65,
    approvedCount: 62,
    createTime: '2024-02-20 14:20:00',
    updateTime: '2024-10-18 09:30:00'
  },
  {
    id: 3,
    name: '手工折纸花束',
    description: '创意手工折纸花束制作，环保材料，永久保存，适合礼品',
    category: '折纸类',
    unitPrice: 8.00,
    stock: 0,
    images: [
      'https://via.placeholder.com/300x300/909399/ffffff?text=折纸花束成品'
    ],
    videoUrl: 'https://via.placeholder.com/video3.mp4',
    accessoriesList: [
      { name: '彩色折纸', quantity: '20张', unit: '包' },
      { name: '铁丝', quantity: '10根', unit: '根' },
      { name: '胶带', quantity: '1卷', unit: '卷' },
      { name: '包装纸', quantity: '2张', unit: '张' }
    ],
    productionTime: 90,
    difficulty: 3,
    tools: ['剪刀', '胶水', '铁丝'],
    status: 0,
    claimedCount: 23,
    completedCount: 18,
    approvedCount: 15,
    createTime: '2024-03-10 16:45:00',
    updateTime: '2024-10-15 11:20:00'
  }
]

const mockCategories = [
  { id: 1, name: '编织类', count: 8 },
  { id: 2, name: '串珠类', count: 12 },
  { id: 3, name: '折纸类', count: 6 },
  { id: 4, name: '刺绣类', count: 4 },
  { id: 5, name: '陶艺类', count: 3 }
]

// 模拟API响应
export const mockAPI = {
  // 模拟管理员登录
  adminLogin: (credentials) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (credentials.username === 'admin' && credentials.password === '123456') {
          resolve({
            code: 200,
            message: '登录成功',
            data: {
              token: 'admin-jwt-token-' + Date.now(),
              user: {
                ...mockUsers[0],
                role: 'admin',
                permissions: ['user:read', 'user:write', 'banner:read', 'banner:write', 'product:read', 'product:write']
              }
            }
          })
        } else {
          reject(new Error('用户名或密码错误'))
        }
      }, 500)
    })
  },

  // 模拟用户登录（保留原有功能）
  login: (credentials) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (credentials.username === 'admin' && credentials.password === '123456') {
          resolve({
            code: 200,
            message: '登录成功',
            data: {
              token: 'mock-jwt-token-' + Date.now(),
              user: mockUsers[0]
            }
          })
        } else {
          reject(new Error('用户名或密码错误'))
        }
      }, 500)
    })
  },

  // 模拟获取用户列表
  getUserList: (params) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const { page = 1, size = 10, username, status, level } = params
        let filteredUsers = [...mockUsers]

        if (username) {
          filteredUsers = filteredUsers.filter(user =>
            user.username.includes(username)
          )
        }

        if (status !== null && status !== undefined) {
          filteredUsers = filteredUsers.filter(user => user.status === status)
        }

        if (level) {
          filteredUsers = filteredUsers.filter(user => user.level === level)
        }

        const total = filteredUsers.length
        const start = (page - 1) * size
        const end = start + size
        const records = filteredUsers.slice(start, end)

        resolve({
          code: 200,
          message: '获取成功',
          data: {
            records,
            total,
            current: page,
            size
          }
        })
      }, 300)
    })
  },

  // 模拟获取用户统计
  getUserStats: () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 200,
          message: '获取成功',
          data: {
            totalUsers: mockUsers.length,
            activeUsers: mockUsers.filter(u => u.status === 1).length,
            newUsers: 1,
            disabledUsers: mockUsers.filter(u => u.status === 0).length
          }
        })
      }, 200)
    })
  },

  // 模拟获取轮播图列表
  getCarouselList: (params) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const { page = 1, size = 10, title, status } = params
        let filteredCarousels = [...mockCarousels]

        if (title) {
          filteredCarousels = filteredCarousels.filter(carousel =>
            carousel.title.includes(title)
          )
        }

        if (status !== null && status !== undefined) {
          filteredCarousels = filteredCarousels.filter(carousel => carousel.status === status)
        }

        // 按排序号排序
        filteredCarousels.sort((a, b) => a.sort - b.sort)

        const total = filteredCarousels.length
        const start = (page - 1) * size
        const end = start + size
        const records = filteredCarousels.slice(start, end)

        resolve({
          code: 200,
          message: '获取成功',
          data: {
            records,
            total,
            current: page,
            size
          }
        })
      }, 300)
    })
  },

  // 模拟获取产品列表
  getProductList: (params) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const { page = 1, size = 10, name, category, status } = params
        let filteredProducts = [...mockProducts]

        if (name) {
          filteredProducts = filteredProducts.filter(product =>
            product.name.includes(name) || product.description.includes(name)
          )
        }

        if (category) {
          filteredProducts = filteredProducts.filter(product => product.category === category)
        }

        if (status !== null && status !== undefined) {
          filteredProducts = filteredProducts.filter(product => product.status === status)
        }

        const total = filteredProducts.length
        const start = (page - 1) * size
        const end = start + size
        const records = filteredProducts.slice(start, end)

        resolve({
          code: 200,
          message: '获取成功',
          data: {
            records,
            total,
            current: page,
            size
          }
        })
      }, 300)
    })
  },

  // 模拟获取产品详情
  getProductDetail: (id) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const product = mockProducts.find(p => p.id === parseInt(id))
        if (product) {
          resolve({
            code: 200,
            message: '获取成功',
            data: product
          })
        } else {
          reject(new Error('产品不存在'))
        }
      }, 200)
    })
  },

  // 模拟获取产品统计
  getProductStats: () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 200,
          message: '获取成功',
          data: {
            totalProducts: mockProducts.length,
            onlineProducts: mockProducts.filter(p => p.status === 1).length,
            offlineProducts: mockProducts.filter(p => p.status === 0).length,
            lowStockProducts: mockProducts.filter(p => p.stock < 50).length,
            outOfStockProducts: mockProducts.filter(p => p.stock === 0).length,
            totalClaimed: mockProducts.reduce((sum, p) => sum + p.claimedCount, 0),
            totalCompleted: mockProducts.reduce((sum, p) => sum + p.completedCount, 0),
            totalApproved: mockProducts.reduce((sum, p) => sum + p.approvedCount, 0)
          }
        })
      }, 200)
    })
  },

  // 模拟获取产品分类
  getProductCategories: () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 200,
          message: '获取成功',
          data: mockCategories
        })
      }, 200)
    })
  },

  // 模拟创建产品
  createProduct: (data) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const newProduct = {
          id: mockProducts.length + 1,
          ...data,
          sales: 0,
          rating: 0,
          reviews: 0,
          createTime: new Date().toISOString().replace('T', ' ').slice(0, 19),
          updateTime: new Date().toISOString().replace('T', ' ').slice(0, 19)
        }
        mockProducts.push(newProduct)

        resolve({
          code: 200,
          message: '创建成功',
          data: newProduct
        })
      }, 500)
    })
  },

  // 模拟更新产品
  updateProduct: (id, data) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const index = mockProducts.findIndex(p => p.id === parseInt(id))
        if (index !== -1) {
          mockProducts[index] = {
            ...mockProducts[index],
            ...data,
            updateTime: new Date().toISOString().replace('T', ' ').slice(0, 19)
          }
          resolve({
            code: 200,
            message: '更新成功',
            data: mockProducts[index]
          })
        } else {
          reject(new Error('产品不存在'))
        }
      }, 500)
    })
  },

  // 模拟删除产品
  deleteProduct: (id) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const index = mockProducts.findIndex(p => p.id === parseInt(id))
        if (index !== -1) {
          mockProducts.splice(index, 1)
          resolve({
            code: 200,
            message: '删除成功'
          })
        } else {
          reject(new Error('产品不存在'))
        }
      }, 500)
    })
  },

  // 模拟产品上架/下架
  updateProductStatus: (id, status) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const product = mockProducts.find(p => p.id === parseInt(id))
        if (product) {
          product.status = status
          product.updateTime = new Date().toISOString().replace('T', ' ').slice(0, 19)
          resolve({
            code: 200,
            message: status === 1 ? '上架成功' : '下架成功',
            data: product
          })
        } else {
          reject(new Error('产品不存在'))
        }
      }, 300)
    })
  },

  // 模拟获取轮播图详情
  getCarouselDetail: (id) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const carousel = mockCarousels.find(c => c.id === parseInt(id))
        if (carousel) {
          resolve({
            code: 200,
            message: '获取成功',
            data: carousel
          })
        } else {
          reject(new Error('轮播图不存在'))
        }
      }, 200)
    })
  },

  // 模拟创建轮播图
  createCarousel: (data) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const newCarousel = {
          id: mockCarousels.length + 1,
          ...data,
          sort: mockCarousels.length + 1,
          createTime: new Date().toISOString().replace('T', ' ').slice(0, 19),
          updateTime: new Date().toISOString().replace('T', ' ').slice(0, 19)
        }
        mockCarousels.push(newCarousel)

        resolve({
          code: 200,
          message: '创建成功',
          data: newCarousel
        })
      }, 500)
    })
  },

  // 模拟更新轮播图
  updateCarousel: (id, data) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const index = mockCarousels.findIndex(c => c.id === parseInt(id))
        if (index !== -1) {
          mockCarousels[index] = {
            ...mockCarousels[index],
            ...data,
            updateTime: new Date().toISOString().replace('T', ' ').slice(0, 19)
          }
          resolve({
            code: 200,
            message: '更新成功',
            data: mockCarousels[index]
          })
        } else {
          reject(new Error('轮播图不存在'))
        }
      }, 500)
    })
  },

  // 模拟删除轮播图
  deleteCarousel: (id) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const index = mockCarousels.findIndex(c => c.id === parseInt(id))
        if (index !== -1) {
          mockCarousels.splice(index, 1)
          // 重新排序
          mockCarousels.forEach((carousel, idx) => {
            carousel.sort = idx + 1
          })
          resolve({
            code: 200,
            message: '删除成功'
          })
        } else {
          reject(new Error('轮播图不存在'))
        }
      }, 500)
    })
  },

  // 模拟轮播图启用/禁用
  updateCarouselStatus: (id, status) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const carousel = mockCarousels.find(c => c.id === parseInt(id))
        if (carousel) {
          carousel.status = status
          carousel.updateTime = new Date().toISOString().replace('T', ' ').slice(0, 19)
          resolve({
            code: 200,
            message: status === 1 ? '启用成功' : '禁用成功',
            data: carousel
          })
        } else {
          reject(new Error('轮播图不存在'))
        }
      }, 300)
    })
  },

  // 模拟批量更新轮播图排序
  updateCarouselSort: (data) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        data.sortArray.forEach(item => {
          const carousel = mockCarousels.find(c => c.id === item.id)
          if (carousel) {
            carousel.sort = item.sort
            carousel.updateTime = new Date().toISOString().replace('T', ' ').slice(0, 19)
          }
        })
        resolve({
          code: 200,
          message: '排序更新成功'
        })
      }, 300)
    })
  },

  // 模拟获取启用的轮播图列表
  getActiveCarouselList: () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const activeCarousels = mockCarousels
          .filter(carousel => carousel.status === 1)
          .sort((a, b) => a.sort - b.sort)

        resolve({
          code: 200,
          message: '获取成功',
          data: activeCarousels
        })
      }, 200)
    })
  }
}